import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

// 普通路由： 无论什么身份访问都会展示
export const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true,
  },

  {
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true,
  },

  // 首页
  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        name: "Dashboard",
        component: () => import("@/views/dashboard/index"),
        meta: { title: "首页", icon: "dashboard" },
      },
    ],
  },
];

// 异步路由： 将会判断展示路由
export const asyncRoutes = [
  // 权限管理
  {
    path: "/nested",
    component: Layout,
    name: "Nested",
    meta: {
      title: "权限管理",
      icon: "el-icon-lock",
    },
    children: [
      {
        path: "user",
        name: "User",
        meta: { title: "用户管理" },
        component: () => import("@/views/nested/user"),
      },
      {
        path: "sole",
        name: "Sole",
        meta: { title: "角色管理" },
        component: () => import("@/views/nested/role"),
      },
      {
        path: "role/auth/:id",
        name: "RoleAuth",
        meta: { activeMenu: "@/views/nested/role", title: "角色授权" },
        component: () => import("@/views/nested/role/roleAuth"),
        hidden: true,
      },
      {
        path: "menu",
        name: "Menu",
        meta: { title: "菜单管理" },
        component: () => import("@/views/nested/menu"),
      },
    ],
  },

  // 商品管理
  {
    path: "/goods",
    name: "Goods",
    component: Layout,
    meta: { title: "商品管理", icon: "el-icon-s-goods" },
    children: [
      {
        path: "branding",
        name: "Branding",
        meta: { title: "品牌管理" },
        component: () => import("@/views/goods/branding"),
      },
      {
        path: "attr",
        name: "Attr",
        meta: { title: "平台属性管理" },
        component: () => import("@/views/goods/attr"),
      },
      {
        path: "spu",
        name: "Spu",
        meta: { title: "Spu管理" },
        component: () => import("@/views/goods/spu"),
      },
      {
        path: "sku",
        name: "Sku",
        meta: { title: "Sku管理" },
        component: () => import("@/views/goods/sku"),
      },
    ],
  },

  // 测试管理
  {
    path: "/page",
    name: "Page",
    component: Layout,
    meta: { title: "测试管理", icon: "el-icon-s-platform" },
    children: [
      {
        path: "test1",
        name: "Test1",
        meta: { title: "测试管理1" },
        component: () => import("@/views/test/test1"),
      },
      {
        path: "test2",
        name: "Test2",
        meta: { title: "测试管理2" },
        component: () => import("@/views/test/test2"),
      },
    ],
  },
];

// 任意路由： 当路径出现错误的时候重定向404
export const anyRoutes = { path: "*", redirect: "/404", hidden: true };

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
